use common_uu::JsonV;
use std::collections::HashMap;

impl CookieParams for &str {
    fn url(&self) -> String {
        self.to_string()
    }

    fn headers(&self) -> HashMap<String, String> {
        Default::default()
    }
}

impl CookieParams for str {
    fn url(&self) -> String {
        self.to_string()
    }

    fn headers(&self) -> HashMap<String, String> {
        Default::default()
    }
}

impl CookieParams for String {
    fn url(&self) -> String {
        self.as_str().url()
    }

    fn headers(&self) -> HashMap<String, String> {
        self.as_str().headers()
    }
}

impl CookieParams for JsonV {
    fn url(&self) -> String {
        self["url"].as_str().unwrap_or_default().to_string()
    }

    fn headers(&self) -> HashMap<String, String> {
        self["headers"]
            .as_object()
            .map(|v| {
                let mut map = HashMap::new();
                for (k, v) in v {
                    map.insert(k.to_string(), v.as_str().unwrap_or_default().to_string());
                }
                map
            })
            .unwrap_or_default()
    }
}

pub trait CookieParams {
    fn url(&self) -> String;
    fn headers(&self) -> HashMap<String, String>;
}
